﻿using PlatypusPlanner.Components.Base;
using PlatypusPlanner.Components.Controllers;
using PlatypusPlanner.Components.Database;
using PlatypusPlanner.Components.Enumerations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PlatypusPlanner.Components.BLL
{
    public class Clubs : BaseBLL
    {
        private ClubController _controller = null;
        public Clubs(PlatypusEntities context = null)
            : base(context)
        {
            _controller = new ClubController(context);
        }

        public pp_club GetByClubName(string clubName)
        {

            return _controller.GetByClubName(clubName);

        }

        public pp_user GetByUserId(pp_user user)
        {

            return _controller.GetByUserId(user);

        }

        #region inserts and updates

        //John: Duplication of Users. I'm still working on this.
        //Creating Method for Creating or Updating User account info
        public pp_club InsertUpdate(pp_club club, pp_user creator, pp_user admin, int clubId, DateTime creationDate, string clubBio, string url, string address, string city, string state, string zip, string clubName, out ClubCreationStatus creationStatus)
        {
            try             // try/catch error handling
            {

                bool allowId = true;         //creation of bool element needed
                allowId = GetByClubName(clubName) == null;  //Check if club exists

                if (allowId == true)     //For when club doesn't exist, and clubId doesn't exist
                {
                    club = pp_club.Createpp_club(0, clubName, DateTime.Now, creator.Id, admin.Id, address, city, state, zip, 1);

                    _controller.Context.pp_club.AddObject(club);
                    _controller.SaveChanges();
                }
                else if (club != null)      //For when club does exist, allows you to chage your settings
                {

                    if(!(GetByUserId(admin)==null))
                    {
                        club.ClubAdminId = admin.Id;
                    }

                    club.Address = address.Trim();
                    club.City = city.Trim();
                    club.ClubBio = clubBio.Trim();
                    club.CreatorId = creator.Id;
                    club.State = state.Trim();
                    club.WebsiteURL = url.Trim();
                    club.Zip = zip.Trim();
                    club.ClubName = clubName.Trim();

                    _controller.SaveChanges();
                }
                
                else
                {

                    //John: I can start exception handling once my enumeration works
                    creationStatus = ClubCreationStatus.DuplicateClubName;     //For when email already excist
                }
                
            }
            catch (Exception ex)  //catch statement to handle any errors that may occur
            {
                creationStatus = ClubCreationStatus.GeneralError;
            }

            creationStatus = ClubCreationStatus.Success;
            return club;
        }
        #endregion

    }
}
